package com.zmh.service.book;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.zmh.common.DateUtil;
import com.zmh.dao.BookDao;
import com.zmh.dao.CatalogDao;
import com.zmh.dao.MessageInfoDao;
import com.zmh.model.book.BookInfo;
import com.zmh.model.book.CatalogInfo;
import com.zmh.model.book.UpdWebsit;
import com.zmh.model.wechat.httpClient.MessageInfo;
import com.zmh.service.httpclient.BookUpdate;
import com.zmh.service.httpclient.Dwz;
import com.zmh.service.wechat.MessageInfoService;

/** 
 * @author zhoumh
 * @version 创建时间：2017年11月16日 下午1:58:36 
 * @Description 
*/
@Service
public class BookUpdateService {
	private static Logger logger = LoggerFactory.getLogger(BookUpdateService.class);
	@Autowired
	private BookDao bookDao;
	@Autowired
	private UpdWebsitService updWebsitService;
	@Autowired
	private CatalogDao catalogDao;
	@Autowired
	private MessageInfoService messageInfoService;
	@Autowired
	private Dwz dwz;
	@Autowired
	private BookUpdate bookUpdate;
	/**
	 * 最新章节的更新信息
	 * @throws UnsupportedEncodingException 
	 * @return: void
	 * @author: zhoumh
	 * @version: 创建时间：2017年11月28日 下午3:38:03 
	 * @Description:
	 */
	public void bookUpdate() {
		//获取书籍更新章节信息
		List<CatalogInfo> catalogInfos = catalogDao.selCatalogByCstatus("0");
		// 记录开始更新的时间戳
		for (CatalogInfo catalogInfo : catalogInfos) {
			//更新章节在十分钟前
			if(DateUtil.getMinDiff(catalogInfo.getUtime()+":000")>10){
				BookInfo book = bookDao.queryBookByUuid(catalogInfo.getBid());
				String bookname = book.getName();
				String catalogName = catalogInfo.getName();
				// 固定的360搜索地址
				String tempUrl = "https://www.so.com/s?ie=utf-8&fr=none&src=360sou_newhome&q="+bookname+"+"+catalogName.replace(" ", "+");
				String soUrl = dwz.mrw(tempUrl);
				String msg = bookname+" 更新章节 "+catalogName+" 链接如下： 360搜索  "+soUrl+" ";
				//获取书籍对应更新站点信息
				List<UpdWebsit> updWebsits = updWebsitService.selWebsitByBid(catalogInfo.getBid());
				//增加一个判断，如果书籍没有更新站点信息就直接生成消息
				for (UpdWebsit updWebsit : updWebsits) {
					//匹配最新章节是否也存在更新
					String catalogurl = updWebsit.getCatalogurl();
					//根据更新站点调用不同的更新方法
					if (catalogurl.indexOf("bqg5200")>0) {
						String longurl = bookUpdate.bqg5200(catalogurl, catalogInfo.getName());
						//调用短地址转换服务
						if(!StringUtils.isEmpty(longurl)){
							msg = msg+updWebsit.getName()+" "+dwz.mrw(longurl)+" ";
						}
						continue;
					}
					if (catalogurl.indexOf("baishuku")>0) {
						String longurl = bookUpdate.baishuku(catalogurl, catalogInfo.getName());
						if(!StringUtils.isEmpty(longurl)){
							msg = msg+updWebsit.getName()+" "+dwz.mrw(longurl)+" ";
						}
						continue;
					}
					if (catalogurl.indexOf("clewx")>0) {
						String longurl = bookUpdate.clewx(catalogurl, catalogInfo.getName());
						if(!StringUtils.isEmpty(longurl)){
							msg = msg+updWebsit.getName()+" "+dwz.mrw(longurl)+" ";
						}
						continue;
					}
					if (catalogurl.indexOf("biquge5200")>0) {
						String longurl = bookUpdate.biquge5200(catalogurl, catalogInfo.getName());
						if(!StringUtils.isEmpty(longurl)){
							msg = msg+updWebsit.getName()+" "+dwz.mrw(longurl)+" ";
						}
						continue;
					}
					if (catalogurl.indexOf("biqukan")>0) {
						String longurl = bookUpdate.biqukan(catalogurl, catalogInfo.getName());
						if(!StringUtils.isEmpty(longurl)){
							msg = msg+updWebsit.getName()+" "+dwz.mrw(longurl)+" ";
						}
						continue;
					}
					if (catalogurl.indexOf("sanjiangge")>0) {
						String longurl = bookUpdate.sanjiangge(catalogurl, catalogInfo.getName());
						if(!StringUtils.isEmpty(longurl)){
							msg = msg+updWebsit.getName()+" "+dwz.mrw(longurl)+" ";
						}
						continue;
					}
				}
				// 增加待发送消息
				String zmh = "oq8Y20dXcYxg4Pk_dI6VVtnLZM0Q";
				MessageInfo messageInfo = new MessageInfo();
				messageInfo.setContent(msg);
				messageInfo.setOpenid(zmh);
				messageInfo.setSendmode("0");
				messageInfo.setType("1");
				messageInfo.setSendtime(DateUtil.getDateTime());
				messageInfoService.insMsg(messageInfo);
				// 变更发送状态
				catalogInfo.setCstatus("1");
				catalogDao.updateCatalog(catalogInfo);
			}
		}
	}
	
	/**
	 * 从书籍源站获取书籍更新
	 */
	public void getBookUpd() {
		//连载的书籍列表
		List<BookInfo> updbooks = bookDao.selBooksByStatus("0");
		// 记录开始更新的时间戳
		String createtime = DateUtil.getDateTime();
		// 获取存储的最新书架信息根据用户UUID
		for (BookInfo bookinfo : updbooks) {
			logger.info("开始更新书籍："+bookinfo.getName());
			// 获取每本书籍的目录页地址
			String catalog = bookinfo.getCatalog();
			// 根据站点信息调用不同的更新方法，暂时写死调用
			if (StringUtils.isEmpty(catalog)) {
				//书籍没有目录页
				continue;
			}
			if (catalog.indexOf("qidian")>0) {
				bookUpdate.qidian(bookinfo);
				continue;
			}
			if (catalog.indexOf("motie")>0) {
				bookUpdate.motie(bookinfo);
				continue;
			}
			if (catalog.indexOf("zongheng")>0) {
				bookUpdate.zongheng(bookinfo);
				continue;
			}
			if (catalog.indexOf("tadu")>0) {
				bookUpdate.tadu(bookinfo);
				continue;
			}
			if (catalog.indexOf("shenqiwang")>0) {
				bookUpdate.shenqiwang(bookinfo);
				continue;
			}
			if (catalog.indexOf("ireader")>0) {
				bookUpdate.ireader(bookinfo);
				continue;
			}
			if (catalog.indexOf("chuangshi")>0) {
				bookUpdate.chuangshi(bookinfo);
				continue;
			}
		}
		//获取本次更新的章节数
		int count = catalogDao.sumCatalogByCreatetime(createtime);
		logger.info("本次更新耗时： "+String.valueOf(DateUtil.getSecDiff(createtime+":000"))+" 秒，共计更新 "+count+" 章节");
	}
}
